package com.nervenets.web.controller.handler.admin.news;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.IdTokenParams;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.news.News;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

public class NewsDeleteHandler extends SessionHandler<IdTokenParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(IdTokenParams params, HttpServletRequest request) throws Exception {
        News news = userDao.findById(params.getTargetId(), News.class);
        if (null == news) return errorMessage(MessageCode.code_404, "您要删除的资讯不存在！");
        userDao.delete(news);
        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
